package com.suyang.syt.controller;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.CitysDTO;
import com.suyang.syt.service.interfaces.CitysService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.JsonUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/citys")
@SuppressWarnings("unchecked")
public class AdminCitysController {
	@Autowired
	private CitysService citysService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "citys/list";
	}

	@RequestMapping(value = "/listTree", method = RequestMethod.POST)
	@ResponseBody
	@AuthMethod(role = "ROLE_PXJG")
	public List<CitysDTO> listTree(HttpServletRequest request, String id) {
		try {
			long cid = 0;
			if (EmptyUtil.isNotEmpty(id)) {
				cid = Long.parseLong(id);
			}
			return citysService.list(cid);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/listGBCT", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj listGBCT(HttpServletRequest request) {
		try {
			ServletContext application = request.getSession().getServletContext();
			List<CitysDTO> list = (List<CitysDTO>) application.getAttribute("listGBCT");
			if (list == null) {
				list = citysService.listGBCT();
				application.setAttribute("listGBCT", JsonUtil.toJson(list));
			}
			return new AjaxObj(1, "国标库行政区域加载完毕！", list);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/findGBCT", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj findGBCT(HttpServletRequest request, String q) {
		try {
			List<CitysDTO> list = citysService.findGBCT(q);
			return new AjaxObj(1, "国标库行政区域加载完毕！", list);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, CitysDTO citysDTO) {
		try {
			return citysService.add(citysDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			return citysService.delete(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, CitysDTO citysDTO) {
		try {
			return citysService.update(citysDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/updateApproved", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updateApproved(HttpServletRequest request, String ids, int status) {
		try {
			return citysService.updateApproved(ids, status);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
